#include "../include/head.h"
#include "../include/threadPool.h"
int exitPipe[2];
void handler(int signum)
{
    printf("signum = %d\n",signum);
    write(exitPipe[1],"1",1);
}

int main(int argc, char *argv[])
{
    pipe(exitPipe);
    if(fork())
    { 
        close(exitPipe[0]);
        signal(SIGUSR1,handler);

        int status;
        wait(&status);


        if(WIFEXITED(status))
        { 
            printf("子线程正常退出！\n");
        }
        else if(WIFSIGNALED(status))
        { 
            int sig = WTERMSIG(status);
            printf("子线程被信号：%d 终止！\n",sig);
            
            switch(sig)
            { 
                case SIGQUIT:
                    printf("终止原因：ctrl + \\ ");
                    break;
                case SIGINT:
                    printf("终止原因：ctrl+c");
                    break;
                case SIGPIPE:
                    printf("终止原因：向无读端管道写or向无sockfd写");
                    break;
                case SIGSEGV:
                    printf("终止原因：段错误\n");
                    break;
                case SIGKILL:
                    printf("终止原因：被强制挂掉\n");
                    break;
                case SIGTERM:
                    printf("终止原因：被通知去挂\n");
                    break;
                default:
                    printf("其他信号：%d\n", sig);
                    break;
            }
        }

        printf(" Parents is going to exit!\n");
        exit(0);
    }
    close(exitPipe[1]);
    ARGS_CHECK(argc,4);
    int workernum = atoi(argv[3]);
    threadPool_t threadPool;
    threadPoolInit(&threadPool,workernum);

    makeWorker(&threadPool);

    int sockfd = tcpInit(argv[1],argv[2]);
    int epfd = epoll_create(1);
    epollAdd(epfd,sockfd);
    epollAdd(epfd,exitPipe[0]);

    struct epoll_event readyset[1024];

    while(1)
    { 
        int readynum = epoll_wait(epfd,readyset,1024,-1);
        for(int i = 0;i<readynum;i++)
        { 
            if(readyset[i].data.fd == sockfd)
            { 
                int netfd = accept(sockfd,NULL,NULL);
                ERROR_CHECK(netfd,-1,"accept");
                printf("master netfd = %d\n",netfd);
                pthread_mutex_lock(&threadPool.mutex);
                tail_insert(&threadPool.taskQueue,netfd);
                pthread_cond_broadcast(&threadPool.cond);
                pthread_mutex_unlock(&threadPool.mutex);
            }
            else if(readyset[i].data.fd == exitPipe[0])
            {  
                //线程要退出了
                printf("threadPool is goning to exit!\n ");
                pthread_mutex_lock(&threadPool.mutex);
                threadPool.exitFlag = 1;
                pthread_cond_broadcast(&threadPool.cond);
                pthread_mutex_unlock(&threadPool.mutex);

                for(int j = 0;j< workernum;j++)
                { 
                    pthread_join(threadPool.workerArr.tidArr[j],NULL);
                }
                printf("master is going to exit!\n");
                exit(0);

            }
        }
    }
    return 0;
}

